package top.lshaci.learning.springboot.mybatisplus.entity;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;
import top.lshaci.learning.springboot.mybatisplus.baseentity.MpTenantEntity;

/**
 * <p>
 *
 * </p>
 *
 * @author lshaci
 * @since 2023-05-31
 */
@Getter
@Setter
@Accessors(chain = true)
@ToString(callSuper = true)
@TableName("sys_simple_sn")
@ApiModel(value = "SimpleSn对象", description = "")
public class SimpleSn extends MpTenantEntity {

    private static final long serialVersionUID = 1L;

    private String tag;

    private String prefix;

    private Integer val;

    public SimpleSn() {
    }

    public SimpleSn(String tag, String prefix) {
        this.tag = tag;
        this.prefix = prefix;
        this.val = 0;
    }

    /**
     * 获取下一个流水号
     *
     * @return 下一个流水号
     */
    public String next() {
        this.val = this.val == 0 ? 1 : this.val + 1;
        String next = StrUtil.fillBefore(String.valueOf(this.val), '0', 3);
        return this.prefix + next;
    }
}
